package org.e_gold.model.crawler.livescore;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang.StringUtils;
import org.e_gold.model.ValueObject;

/**
 * @author Thang Nguyen
 */
@Entity
@Table(name = "CEvent")
public class CEvent extends ValueObject implements Serializable, Comparable<CEvent>{

	private static final long	serialVersionUID	= 1L;

	@Id
	@GeneratedValue
	private Long id;

	@Column(name = "home_name")
	private String homeName;

	@Column(name = "away_name")
	private String awayName;

	@ManyToOne
	@JoinColumn(name="league_id")
	private League league;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "play_date")
	private Date play_date;

	private boolean status = false;

	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public CEvent(){
		this("", "", null);
	}

	public CEvent(String homeName, String awayName){
		this(homeName, awayName, null);
	}

	public CEvent(String homeName, String awayName, League league) {
		this(homeName, awayName, league, null);
	}

	public CEvent(String homeName, String awayName, League league, Date play_date) {
		this.homeName = homeName;
		this.awayName = awayName;
		this.league = league;
		this.play_date = play_date;
	}

	@Override
	public int compareTo(CEvent o) {
		if(StringUtils.equalsIgnoreCase(homeName, o.getHomeName())){
			if(StringUtils.equalsIgnoreCase(awayName, o.getAwayName())){
				return 0;
			}
		}
		if(homeName.compareTo(o.getHomeName()) == 0){
			return awayName.compareTo(o.getAwayName());
		}else{
			return homeName.compareTo(o.getHomeName());
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((awayName == null) ? 0 : awayName.hashCode());
		result = prime * result
				+ ((homeName == null) ? 0 : homeName.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((league == null) ? 0 : league.hashCode());
		result = prime * result
				+ ((play_date == null) ? 0 : play_date.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof CEvent)) {
			return false;
		}
		CEvent other = (CEvent) obj;
		if (awayName == null) {
			if (other.awayName != null) {
				return false;
			}
		} else if (!awayName.equals(other.awayName)) {
			return false;
		}
		if (homeName == null) {
			if (other.homeName != null) {
				return false;
			}
		} else if (!homeName.equals(other.homeName)) {
			return false;
		}
		return true;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getHomeName() {
		return homeName;
	}

	public void setHomeName(String homeName) {
		this.homeName = homeName;
	}

	public String getAwayName() {
		return awayName;
	}

	public void setAwayName(String awayName) {
		this.awayName = awayName;
	}

	public League getLeague() {
		return league;
	}

	public void setLeague(League league) {
		this.league = league;
	}

	public Date getPlay_date() {
		return play_date;
	}

	public void setPlay_date(Date play_date) {
		this.play_date = play_date;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("[awayname='");
		sb.append(awayName);
		sb.append("', homeName='");
		sb.append(homeName);
		sb.append("', play_date='");
		sb.append(play_date);
		sb.append("']");

		return sb.toString();
	}
}
